﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codinfinity.BeIDApplet
{
    public class IdentityObject
    {
        public string NationalNumber { get; set; }
        public string Gender { get; set; }
        public string Nationality { get; set; }
        public string PlaceOfBirth { get; set; }
        public string DateOfBirth { get; set; }
        public string Address { get; set; }
        public string Locality { get; set; }
        public string PostalCode { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Age { get; set; }
        public bool Identified { get; set; }
        public string Hash { get; set; }

        public string enPack()
        {
            string str = "";
            var pr = this.GetType().GetProperties();
            foreach (var p in pr)
            {
                str += p.GetValue(this, null);
                if (pr.Last() != p)
                    str += ",";
            }

            return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(str));
        }

        public void dePack(string str)
        {
            var pr = this.GetType().GetProperties().ToList();
            byte[] b = Convert.FromBase64String(str);
            List<string> propvals = System.Text.Encoding.UTF8.GetString(b, 0, b.Length).Split(',').ToList();
            foreach (var p in pr)
            {
                if (p.PropertyType.Equals(typeof(string)))
                    p.SetValue(this, propvals[pr.IndexOf(p)], null);
                if (p.PropertyType.Equals(typeof(bool)))
                    p.SetValue(this, propvals[pr.IndexOf(p)]=="True", null);
            }
        }
    }
}
